package com.ruoyi.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysPostService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@RestController
@RequestMapping("/system/post")
public class SysPostController extends BaseController {
    @Autowired
    ISysPostService  iSysPostService;
@Autowired
    ISysDictDataService iSysDictDataService;
    @GetMapping("/list")//全查
    public TableDataInfo selectByPost(SysPost sysPost){
        startPage();
        List<SysPost> sysPosts = iSysPostService.selectPostList(sysPost);

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(sysPosts);
        tableDataInfo.setTotal(new PageInfo(sysPosts).getTotal());
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查詢");
        return getDataTable(sysPosts);
    }


    //导出
 /*   @PostMapping("/export")
    public void  exec(HttpServletResponse response, SysPost post){
        List<SysPost> sysPosts = iSysPostService.selectPostList(post);
        ExcelUtil<SysPost> excelUtil = new ExcelUtil(SysPost.class);
        excelUtil.exportExcel(response,sysPosts,"岗位管理");

    }*/

    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPost sysPost) throws Exception{
        List<SysPost> lista=iSysPostService.selectPostList(sysPost);
        exec(lista,response);
    }

    public void exec(List<SysPost>SysPost, HttpServletResponse response) throws Exception {
        XSSFWorkbook xssfWorkbook=new XSSFWorkbook();
        XSSFSheet sheet = xssfWorkbook.createSheet("岗位");

        XSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("岗位序号");
        row.createCell(1).setCellValue("岗位名称");
        row.createCell(2).setCellValue("岗位排序");

        row.createCell(3).setCellValue("状态");
        row.createCell(4).setCellValue("备注");
        row.createCell(5).setCellValue("创建时间");


        for (int i = 0; i < SysPost.size(); i++) {

            SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
            Date d = sdf.parse(SysPost.get(i).getCreateTime().toString());
            SimpleDateFormat formatDatee = new SimpleDateFormat("yyyy-MM-dd");
            String formatDate=formatDatee.format(d);


            XSSFRow row1 = sheet.createRow(i+1);
            row1.createCell(0).setCellValue(SysPost.get(i).getPostId());
            row1.createCell(1).setCellValue(SysPost.get(i).getPostName());
            row1.createCell(2).setCellValue(SysPost.get(i).getPostCode());
            row1.createCell(3).setCellValue(SysPost.get(i).getPostSort());
            row1.createCell(4).setCellValue(SysPost.get(i).getStatus());
            row1.createCell(5).setCellValue(formatDate);
        }
        ServletOutputStream outputStream = response.getOutputStream();
        xssfWorkbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
        xssfWorkbook.close();
    }

    //单查
    @GetMapping(value = "/{postId}")
    public AjaxResult pid(@PathVariable Long postId){
        return AjaxResult.success(iSysPostService.selectPostById(postId));
    }
    //新增
    @PostMapping
    public Integer postinsert(@Validated @RequestBody SysPost sysPost){
        sysPost.setCreateBy(getUsername());
        int i = iSysPostService.insertPost(sysPost);
        return i;
    }
/*    @DeleteMapping("/{postId}")//单删除
    public Integer postdelete(@PathVariable Long postId){
        int i = iSysPostService.deletePostById(postId);
        return i;
    }*/
@PutMapping//修改
    public Integer postupdate(@Validated @RequestBody SysPost sysPost){
    sysPost.setUpdateBy(getUsername());
        int i = iSysPostService.updatePost(sysPost);
        return i;
    }
    @DeleteMapping("/{postId}")//多删除
    public AjaxResult postdeleteall(@PathVariable Long[] postId){
        return toAjax(iSysPostService.deletePostByIds(postId));
    }

}
